package com.northpool.operator.analysis.dataset;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.common.stpclient.StpClientOperatorBase;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.resources.command.QueryFilter;
import com.northpool.resources.datasource.IDataSource;
import com.northpool.resources.datatable.dao.IMapDAO;
import com.northpool.resources.datatable.operate.ITableOperator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@OperatorInfo(
        ref = "RowUpdateOperator",
        name = "数据集行数据更新",
        description = ""
)
public class RowUpdateOperator extends StpClientOperatorBase {
    String dataSourceId;
    String tableName;
    String keyField;
    String keyValue;
    String updateField;
    String updateValue;

    @InputParams({
            @InputParam(ref = "dataSourceId", name = "数据源ID", desc = "数据源ID", type = TypeEnum.DATASOURCE, required = true),
            @InputParam(ref = "tableName", name = "数据表名称", desc = "数据表名称，如: public.table", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "keyField", name = "主键字段", desc = "主键字段", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "keyValue", name = "主键值", desc = "主键值", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "updateField", name = "更新字段", desc = "更新字段", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "updateValue", name = "更新值", desc = "更新值", type = TypeEnum.STRING, required = true),
    })
    public RowUpdateOperator() {
    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(RowUpdateOperator.class ,TestParamBuilder.aBuilder()
                .param("dataSourceId", "bcgtest")
                .param("tableName", "result.历年统计数据")
                .param("keyField", "year")
                .param("keyValue", "2021")
                .param("updateField", "value")
                .param("updateValue", "1%")
                .build());
    }

    @Override
    public void execute(ParamTool params) throws Exception {
        this.dataSourceId = params.getStr("dataSourceId");
        this.tableName = params.getStr("tableName");
        this.keyField = params.getStr("keyField");
        this.keyValue = params.getStr("keyValue");
        this.updateField = params.getStr("updateField");
        this.updateValue = params.getStr("updateValue");

//        MetaUtils metaUtils = new MetaUtils();
//        metaUtils.init("meta.link.to", 8182, "meta.link.to:2181");

        String dataSourceJson = DatabaseUtils.getDataSourceJson(this.dataSourceId);
        IDataSource dataSource = IDataSource.fromJSON(dataSourceJson);

        String[] fields = new String[]{this.keyField, this.updateField};
        ITableOperator tableOperator = dataSource.getTableOperator(this.tableName);
        IMapDAO<Object> dao = tableOperator.getTable().mapDao();
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.addSQL(this.keyField + "= '" + this.keyValue + "'");
        queryFilter.setOutputFieldNames(fields);
        List<Object[]> results = dao.queryArray(queryFilter, 1);

        Map<String, Object> fieldValMap = new HashMap<>();
        fieldValMap.put(this.keyField, this.keyValue);
        fieldValMap.put(this.updateField, this.updateValue);

        if (results.isEmpty()) {
            // 新增数据
            dao.insert(fieldValMap);
        } else {
            // 存在数据就更新
            dao.update(fieldValMap);
        }
    }

}
